package com.zl.smartmall.library.os.po;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 版本检查数据封装类
 * 
 * @author 陈希然
 * 
 */
public class CheckVersionInfo {

	private boolean versionLow;
	private int versionCode;
	private String versionName;
	private String versionIntro;
	private String downloadUrl;

	public boolean isVersionLow() {
		return versionLow;
	}

	public void setVersionLow(boolean versionLow) {
		this.versionLow = versionLow;
	}

	public int getVersionCode() {
		return versionCode;
	}

	public void setVersionCode(int versionCode) {
		this.versionCode = versionCode;
	}

	public String getVersionName() {
		return versionName;
	}

	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	public String getVersionIntro() {
		return versionIntro;
	}

	public void setVersionIntro(String versionIntro) {
		this.versionIntro = versionIntro;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}
	
	public static CheckVersionInfo parse(String versionName, JSONObject jsonObject) throws JSONException {
		if(jsonObject != null && jsonObject.length() > 0) {
			CheckVersionInfo checkVersionInfo = new CheckVersionInfo();
			checkVersionInfo.downloadUrl = jsonObject.getString("url");
			checkVersionInfo.versionCode = jsonObject.getInt("ver_code");
			checkVersionInfo.versionIntro = jsonObject.getString("wsnew");
			checkVersionInfo.versionName = jsonObject.getString("version");
			checkVersionInfo.versionLow = checkVersion(versionName, checkVersionInfo.versionName);
			return checkVersionInfo;
		}
		
		return null;
	}
	
	/**
	 * 判断当前版本是否比服务器上的版本低
	 * @param localVersion 本地版本号
	 * @param serverVersion 服务器版本号
	 * @return
	 */
	private static Boolean checkVersion(String localVersion, String serverVersion){
		String localVersionSplit = localVersion.replace(".", "");
		String ServerVersionSplit = serverVersion.replace(".", "");
		//1.0.2  1.2 0.0.0.1
		int compareTo = ServerVersionSplit.compareTo(localVersionSplit);
		if(compareTo > 0){
			return true;
		} else {
			return false;
		}
		
//		if(Integer.valueOf(nowVer_array)>=Integer.valueOf(ServerVer_array)){
//			return false;
//		}else{
//			return true;
//		}
	}

}
